require 'test/unit'
require 'observable'

class TestObservable < Test::Unit::TestCase
	class Target
		extend ObservableField
		observable_field :obs1, :obs2

		def run1(*args); obs1.notify(*args); end
		def run2(*args); obs2.notify(*args); end
	end

	def setup
		@target = Target.new
		@result = ''
		@target.obs1.add_observer { |arg1,arg2| @result << "1,1=>#{arg1.to_s}-#{arg2.to_s}"}
		@target.obs1.add_observer { |arg1,arg2| @result << "1,2=>#{arg1.to_s}-#{arg2.to_s}"}
		@target.obs1.add_observer { |arg1,arg2| @result << "1,3=>#{arg1.to_s}-#{arg2.to_s}"}
		@target.obs2.add_observer { |arg| @result << "2,1=>#{arg.to_s}"}
	end

	def test_add
		assert_equal(3, @target.obs1.count_observers)
		assert_equal(1, @target.obs2.count_observers)
	end

	def test_notify
		@target.run2('hi')
		assert_equal('2,1=>hi', @result)
		@result = ''
		@target.run1('hi','bye')
		assert_equal('1,1=>hi-bye1,2=>hi-bye1,3=>hi-bye', @result) 
	end
end
